package cn.jussi.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"*.do" })
public class TagController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String mn = req.getServletPath();
		mn = mn.substring(1, mn.length()-3);
		try {
			Method method = this.getClass().getDeclaredMethod(mn, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

	@SuppressWarnings("unused")
	private void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String, Integer> map = new HashMap<>();
		map.put("张三", 20);
		map.put("李四", 21);
		map.put("王五", 22);
		
		req.setAttribute("map", map);
		
		req.getRequestDispatcher("/index.jsp").forward(req, resp);
	}
	
	@SuppressWarnings("unused")
	private void jstl4(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<String> list = new ArrayList<>();
		list.add("张三");
		list.add("李四");
		list.add("王五");
		
		req.setAttribute("user", list);
		
		req.getRequestDispatcher("/jstl4.jsp").forward(req, resp);
	}
	
	@SuppressWarnings("unused")
	private void jstl5(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("date", new Date());	
		req.getRequestDispatcher("/jstl5.jsp").forward(req, resp);
	}
}
